package com.haven.webrobotserver.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.haven.webrobotserver.domain.query.TestcaseListVOQuery;
import com.haven.webrobotserver.domain.vo.TestcaseVO;
import com.haven.webrobotserver.service.TestcaseService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin("http://localhost:8080")
@RequestMapping("/testcase")
public class TestcaseController {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private TestcaseService testcaseService;

    @RequestMapping("/getVOlist")
    public JSONObject getVOlist(@RequestBody TestcaseListVOQuery testcaseListVOQuery){
        JSONObject result = new JSONObject();

        logger.info(testcaseListVOQuery.toString());

        try{
            List<TestcaseVO> testcaseVOList = testcaseService.getTestcaseVOList(testcaseListVOQuery);
            String s = JSON.toJSONString(testcaseVOList);
            JSONArray lists = JSON.parseArray(s);
            result.put("data",lists);
            result.put("status",200);
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","获取测试用例列表失败");
        }

        return result;
    }

    @RequestMapping("/newTestcase")
    public JSONObject newTestcase(@RequestBody TestcaseVO testcaseVO){
        JSONObject result = new JSONObject();


        try{
            testcaseService.newTestcase(testcaseVO);
            result.put("status",200);
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
        }

        return result;
    }

    @RequestMapping("/getTestcaseVObyId")
    public JSONObject getTestcaseVObyId(@RequestParam(name = "id") int id){
        JSONObject result = new JSONObject();

        try{
            TestcaseVO tc = testcaseService.getTestcaseVObyId(id);

            String s = JSON.toJSONString(tc);
            Object parse = JSON.parse(s);

            result.put("data",parse);
            result.put("status",200);
            result.put("msg","获取testcasevo成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","获取testcasevo失败");
        }

        return result;
    }

    @RequestMapping("/updateTestcase")
    public JSONObject updateTestcase(@RequestBody TestcaseVO testcaseVO){
        JSONObject result = new JSONObject();
        logger.info(testcaseVO.toString());
        try{
            testcaseService.updateTestcase(testcaseVO);
            result.put("status",200);
            result.put("msg","保存测试用例成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","保存测试用例赵失败");
        }

        return result;
    }

    @RequestMapping("/deleteOne")
    public JSONObject deleteOne(@RequestParam(name="id") int id){
        JSONObject result = new JSONObject();

        try{
            testcaseService.deleteOne(id);
            result.put("status",200);
            result.put("msg","删除测试用例成功");
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
            result.put("msg","删除测试用例失败");
        }


        return result;
    }

    @GetMapping("/getTestcaseListByProjectId")
    public JSONObject getTestcaseListByProjectId(@RequestParam(name="projectId") int projectId){
        JSONObject result = new JSONObject();
        try{
            List<Map<String, String>> testcaseListByProjectId = testcaseService.getTestcaseListByProjectId(projectId);
            result.put("data",testcaseListByProjectId);
            result.put("status",200);
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",500);
        }

        return result;
    }
}
